/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.faytechcc.csc251.gui;

import edu.faytechcc.csc251.data.Parse;
import edu.faytechcc.csc251.data.SFXTConnection;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

/**
 *
 * @author Joshua
 */
public class CharacterData extends JFrame {
    
    private ComboBoxModel[] model = new ComboBoxModel[3];
    private TableModel[] tableModel = new TableModel[1];
    private String[] teamOption;
    private String[] teamSF;
    private String[] teamTK;
    private String[] defaultComboBox = {"N/A"};
    private JComboBox options;
    private JComboBox characters;
    private JTable table;
    private JScrollPane scrollPane;
    private JButton submit;
    private JPanel mainPanel;
    private JPanel northPanel;
    private JPanel centerPanel;
    private ImageIcon image;
    private JLabel label;
    private ResultSet results;
    private String[][] tableData;
    private String[] colNames;
    private int numberOfResults;
    
    public CharacterData() throws IOException{
        this.setTitle("Street Fighter X Tekken 2013 Character Frame Data");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        results = SFXTConnection.getTable("Glossary");
        getRowNumbers(results);
        buildTable();
        buildComboBoxes();
        buildMainPanel();
        this.add(mainPanel);
        this.pack();
        this.setVisible(true);
    }
    
    private void buildMainPanel() {
        mainPanel = new JPanel(new BorderLayout());
        northPanel = new JPanel();
        centerPanel = new JPanel();
        
        options = new JComboBox(teamOption);
        options.setPreferredSize(new Dimension(150, 25));
        options.addActionListener(new optionsActionListener());
        
        characters = new JComboBox();
        characters.setModel(model[0]);
        characters.setPreferredSize(new Dimension(150, 25));
        
        submit = new JButton("Submit");
        submit.addActionListener(new submitButtonActionListener());
        
        northPanel.add(options);
        northPanel.add(characters);
        northPanel.add(submit);
        
        image = new ImageIcon("images//Logo_sfxt.jpg");
        label = new JLabel(image);
        
        table = new JTable(tableData, colNames);
        table.getColumnModel().getColumn(0).setPreferredWidth(200);
        table.getColumnModel().getColumn(1).setPreferredWidth(500);
        scrollPane = new JScrollPane(table);
        scrollPane.setHorizontalScrollBarPolicy(
                ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
        
        centerPanel.add(scrollPane);
        
        mainPanel.add(northPanel, BorderLayout.NORTH);
        mainPanel.add(label, BorderLayout.WEST);
        mainPanel.add(centerPanel, BorderLayout.CENTER);
        
    }
    
    private void getRowNumbers(ResultSet table)
    {
        try {
            table.last();
            numberOfResults = table.getRow();
            table.first();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Could not open table");
            System.exit(0);
        }
    }
    private void buildComboBoxes() throws IOException
    {
        teamOption = Parse.parse("Options.csv");
        teamSF = Parse.parse("StreetFighter.csv");
        teamTK = Parse.parse("Tekken.csv");
        
        model[0] = new DefaultComboBoxModel(defaultComboBox);
        model[1] = new DefaultComboBoxModel(teamSF);
        model[2] = new DefaultComboBoxModel(teamTK);
    }
    
    private void buildTable() {
        try {
            ResultSetMetaData meta = results.getMetaData();
            colNames = new String[meta.getColumnCount()];
            for (int index = 0; index < colNames.length; index++) {
                colNames[index] = meta.getColumnLabel(index + 1);
            }

            tableData =
                    new String[numberOfResults][colNames.length];

            for (int row = 0; row < numberOfResults; row++) {
                for (int col = 0; col < colNames.length; col++) {
                    tableData[row][col] = results.getString(col + 1);
                }
                results.next();
            }
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(this, "Opps somethig wrong with meta data");
            System.exit(999);
        }
    }
    
    class optionsActionListener implements ActionListener
    {

        @Override
        public void actionPerformed(ActionEvent ae)
        {
            switch(options.getSelectedItem().toString())
            {
                case "Street Fighter":
                    characters.setModel(model[1]);
                    break;
                case "Tekken":
                    characters.setModel(model[2]);
                    break;
                default:
                    characters.setModel(model[0]);
                    break;
            }
        }     
    }
    
    class submitButtonActionListener implements ActionListener
    {

        @Override
        public void actionPerformed(ActionEvent ae) 
        {
            switch (options.getSelectedItem().toString()) {
                case "Glossary":
                    image = new ImageIcon("images//Logo_sfxt.jpg");
                    label.setIcon(image);
                    results = SFXTConnection.getTable("Glossary");
                    getRowNumbers(results);
                    buildTable();
                    tableModel[0] = new DefaultTableModel(tableData, colNames);
                    table.setModel(tableModel[0]);
                    table.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
                    table.getColumnModel().getColumn(0).setPreferredWidth(200);
                    table.getColumnModel().getColumn(1).setPreferredWidth(500);
                    pack();
                    break;
                case "Misc. Data":
                    image = new ImageIcon("images//Logo_sfxt.jpg");
                    label.setIcon(image);
                    results = SFXTConnection.getTable("Misc.Data");
                    getRowNumbers(results);
                    buildTable();
                    tableModel[0] = new DefaultTableModel(tableData, colNames);
                    table.setModel(tableModel[0]);
                    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
                    table.getColumnModel().getColumn(0).setPreferredWidth(150);
                    table.getColumnModel().getColumn(1).setPreferredWidth(75);
                    table.getColumnModel().getColumn(2).setPreferredWidth(75);
                    table.getColumnModel().getColumn(3).setPreferredWidth(75);
                    table.getColumnModel().getColumn(4).setPreferredWidth(75);
                    table.getColumnModel().getColumn(5).setPreferredWidth(75);
                    table.getColumnModel().getColumn(6).setPreferredWidth(50);
                    table.getColumnModel().getColumn(7).setPreferredWidth(100);
                    table.getColumnModel().getColumn(8).setPreferredWidth(100);
                    table.getColumnModel().getColumn(9).setPreferredWidth(75);
                    pack();
                    break;
                default:
                    String character = characters.getSelectedItem().toString();
                    image = new ImageIcon("images//" + character + "_sfxt.jpg");
                    label.setIcon(image);        
                    results = SFXTConnection.getTable(character);
                    getRowNumbers(results);
                    buildTable();
                    tableModel[0] = new DefaultTableModel(tableData, colNames);
                    table.setModel(tableModel[0]);
                    table.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
                    table.getColumnModel().getColumn(0).setPreferredWidth(220);
                    table.getColumnModel().getColumn(1).setPreferredWidth(250);
                    table.getColumnModel().getColumn(2).setPreferredWidth(75);
                    table.getColumnModel().getColumn(3).setPreferredWidth(150);
                    table.getColumnModel().getColumn(4).setPreferredWidth(75);
                    table.getColumnModel().getColumn(5).setPreferredWidth(350);
                    table.getColumnModel().getColumn(6).setPreferredWidth(150);
                    table.getColumnModel().getColumn(7).setPreferredWidth(150);
                    table.getColumnModel().getColumn(8).setPreferredWidth(75);
                    table.getColumnModel().getColumn(9).setPreferredWidth(200);
                    table.getColumnModel().getColumn(10).setPreferredWidth(1000);
                    pack();
                    break;
            }
        }
        
    }
}
